package ModelLayer;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class DataStorage {

	private CustomerRecords customerRecords;
	private ContractorRecords contractorRecords;
	private EmployeeRecords employeeRecords;
	private GroupRecords groupRecords;
	private SaleItemRecords saleItemRecords;
	private LeaseItemRecords leaseItemRecords;
	private SalesRecords salesRecords;
	private LeaseRecords leaseRecords;
	private FileInputStream f_in;
	private ObjectInputStream obj_in;
	private FileOutputStream f_out;
	private ObjectOutputStream obj_out;

	public DataStorage() throws IOException {
		customerRecords = CustomerRecords.getInstance();
		contractorRecords = ContractorRecords.getInstance();
		employeeRecords = EmployeeRecords.getInstance();
		groupRecords = GroupRecords.getInstance();
		saleItemRecords = SaleItemRecords.getInstance();
		leaseItemRecords = LeaseItemRecords.getInstance();
		salesRecords = SalesRecords.getInstance();
		leaseRecords = LeaseRecords.getInstance();
	}

	public boolean openInFile() {
		try {
			f_in = new FileInputStream("DataStorage.ser");
			obj_in = new ObjectInputStream(f_in);
		} catch (IOException ioException) {
			return false;
		}
		return true;
	}

	public void closeInFile() {
		try {
			obj_in.close();
		} catch (IOException ioException) {
		}
	}

	public void openOutFile() {
		try {
			f_out = new FileOutputStream("DataStorage.ser", false);
			obj_out = new ObjectOutputStream(f_out);
		} catch (IOException ioException) {
		}
	}

	public void closeOutFile() {
		try {
			obj_out.close();
		} catch (IOException ioException) {
		}
	}

	public void saveAll() throws IOException {
		openOutFile();
		saveGroups();
		saveCustomers();
		saveContractors();
		saveEmployees();
		saveSaleItems();
		saveLeaseItems();
		saveSales();
		saveLeases();
		closeOutFile();
	}

	public void loadAll() throws IOException, ClassNotFoundException {
		if (openInFile()) {
			loadGroups();
			loadCustomers();
			loadContractors();
			loadEmployees();
			loadSaleItems();
			loadLeaseItems();
			loadSales();
			loadLeases();
			closeInFile();
		}
	}

	public void saveLeases() throws IOException {

		leaseRecords.save(obj_out);

	}

	public void loadLeases() throws IOException, ClassNotFoundException {

		leaseRecords.load(obj_in);

	}

	public void saveGroups() throws IOException {
		groupRecords.save(obj_out);

	}

	public void loadGroups() throws IOException, ClassNotFoundException {

		groupRecords.load(obj_in);

	}

	public void saveSales() throws IOException {

		salesRecords.save(obj_out);

	}

	public void loadSales() throws IOException, ClassNotFoundException {

		salesRecords.load(obj_in);

	}

	public void saveCustomers() throws IOException {

		customerRecords.save(obj_out);

	}

	public void loadCustomers() throws IOException, ClassNotFoundException {

		customerRecords.load(obj_in);

	}

	public void saveContractors() throws IOException {

		contractorRecords.save(obj_out);

	}

	public void loadContractors() throws IOException, ClassNotFoundException {

		contractorRecords.load(obj_in);

	}

	public void saveEmployees() throws IOException {

		employeeRecords.save(obj_out);

	}

	public void loadEmployees() throws IOException, ClassNotFoundException {

		employeeRecords.load(obj_in);

	}

	public void saveSaleItems() throws IOException {

		saleItemRecords.save(obj_out);

	}

	public void loadSaleItems() throws IOException, ClassNotFoundException {

		saleItemRecords.load(obj_in);

	}

	public void saveLeaseItems() throws IOException {

		leaseItemRecords.save(obj_out);

	}

	public void loadLeaseItems() throws IOException, ClassNotFoundException {

		leaseItemRecords.load(obj_in);

	}

}
